package com.hkbigdata.transform;

import com.hkbigdata.bean.WaterSensor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.List;

/**
 * @author liuanbo
 * @creat 2024-04-12-10:56
 * @see 2194550857@qq.com
 */
public class Flink10_TransForm_Process_Anonymous {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        List<WaterSensor> waterSensors = Arrays.asList(
                new WaterSensor("ws_001", 1607527992000L, 20.0),
                new WaterSensor("ws_001", 1607527992001L, 22.2),
                new WaterSensor("ws_001", 1607527992002L, 33.0),
                new WaterSensor("ws_002", 1607527992003L, 44.0),
                new WaterSensor("ws_002", 1607527992004L, 22.0),
                new WaterSensor("ws_002", 1607527992001L, 50.0)
        );

        env.fromCollection(waterSensors)
/*                .process(new ProcessFunction<WaterSensor, Tuple2<String, Double>>() {
                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<Tuple2<String, Double>> out) throws Exception {
                        out.collect(new Tuple2<>(value.getId(), value.getVc()));
                    }
                }).print();*/
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, Tuple2<String, Double>>() {
                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<Tuple2<String, Double>> out) throws Exception {
                        out.collect(new Tuple2<>(ctx.getCurrentKey(), value.getVc()));
                    }
                }).print();

        env.execute();


    }
}
